package cn.elead.chaos.remote.web.remote.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.constant.UserConstants;
import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.sso.core.util.SsoUtils;
import cn.elead.chaos.remote.web.remote.domain.SysConfig;
import cn.elead.chaos.remote.web.remote.service.ISysConfigService;

/**
 * 参数配置 信息操作处理
 * 
 * @author luopeng
 */
@UnifiedReturn
@RestController
@RequestMapping("/system/config")
public class RemoteConfigController extends BaseController {

	@Autowired
	private ISysConfigService configService;

	/**
	 * 获取参数配置列表
	 */
	@GetMapping("/page")
	public TableDataInfo<SysConfig> page(SysConfig config) {
		startPage();
		List<SysConfig> list = configService.selectConfigList(config);
		return getDataTable(list);
	}

	@GetMapping("/export")
	@Log(title = "参数管理", businessType = BusinessType.EXPORT)
	public String export(SysConfig config) {
		List<SysConfig> list = configService.selectConfigList(config);
		ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
		return util.exportExcel(list, "参数数据");
	}

	/**
	 * 根据参数编号获取详细信息
	 */
	@GetMapping(value = "/{configId}")
	public SysConfig getInfo(@PathVariable Long configId) {
		SysConfig selectConfigById = configService.selectConfigById(configId);
		return selectConfigById;
	}

	/**
	 * 根据参数键名查询参数值
	 */
	@GetMapping(value = "/configKey/{configKey}")
	public String getConfigKey(@PathVariable String configKey) {
		String selectConfigByKey = configService.selectConfigByKey(configKey);
		return selectConfigByKey;
	}

	/**
	 * 新增参数配置
	 */
	@PostMapping
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	public int add(@Validated @RequestBody SysConfig config) {
		if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增参数'" + config.getConfigName() + "'失败，参数键名已存在"));
		}
		config.setCreateBy(SsoUtils.getUserName());
		int insertConfig = configService.insertConfig(config);
		return insertConfig;
	}

	/**
	 * 修改参数配置
	 */
	@PutMapping
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	public int edit(@Validated @RequestBody SysConfig config) {
		if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改参数'" + config.getConfigName() + "'失败，参数键名已存在"));
		}
		config.setUpdateBy(SsoUtils.getUserName());
		int updateConfig = configService.updateConfig(config);
		return updateConfig;
	}

	/**
	 * 删除参数配置
	 */
	@DeleteMapping("/{configIds}")
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	public int remove(@PathVariable Long[] configIds) {
		int deleteConfigByIds = configService.deleteConfigByIds(configIds);
		return deleteConfigByIds;
	}
}
